<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mis_data_perangkat1 extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('mis_tambah_perangkat_model', 'INVENTORY_PERANGKAT');
		$this->secure_session();
	}

	function index() {
		$data['title'] = "Permintaan Penambahan Perangkat";
		$data['it_app']=$this->INVENTORY_PERANGKAT->GetApp();
		$data['it_app2']=$this->INVENTORY_PERANGKAT->GetBarang();
		$data['unitkerja']=$this->INVENTORY_PERANGKAT->Departemen();

		

		viewByTemplate('mis_data_perangkat1/index', $data, 'grid');
	}

	function id_auto(){
		$query1 = $this->db->query("SELECT id_pengadaan FROM (SELECT * FROM mis_pengadaan ORDER BY id_pengadaan DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_pengadaan'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_pengadaan_auto'] = $autonum1;

		$query = $this->db->query("SELECT kode_pengadaan FROM (SELECT * FROM mis_pengadaan ORDER BY kode_pengadaan DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_pengadaan'];
		else
			$MaxID = "KP0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_pengadaan_auto'] = "KP" . sprintf("%07s",$NextID);

		echo json_encode($data);
	}

	function getdata() {
		$this->secure_session();
		echo $this->INVENTORY_PERANGKAT->GetJSON();
		
	}
	
	function getdata2() {
		$this->secure_session();
		echo $this->INVENTORY_PERANGKAT->GetJSON2();
		
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");


		// Validation
		if (post("id_pengadaan") == '') {
			$error->Add("id_pengadaan", "Primary Key", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM sample_master WHERE UPPER(pk_master) = '".@strtoupper(@trim(post("pk_master")))."'")) > 0) {
			$error->Add("id_pengadaan", "Primary Key", $tpl_duplicate);
		} else if (post("nama_pengaju") == '') {
			$error->Add("nama_pengaju", "Nama Pengaju", $tpl_empty);
		} else if (post("nama_unit") == '') {
			$error->Add("nama_unit", "Departemen", $tpl_empty);
		} else if (post("tanggal_pengajuan") == '') {
			$error->Add("tanggal_pengajuan", "Tanggal Pengajuan", $tpl_empty);
		} else if (post("jumlah_barang") == '') {
			$error->Add("jumlah_barang", "Jumlah Permintaan", $tpl_empty);
		}
		
		return $error;
	}

	function ajax_post() {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				// Fill Post Data
				
				$data	=  array(
								'id_pengadaan' => $this->input->post('id_pengadaan'),
								'kode_pengadaan' => $this->input->post('kode_pengadaan'),
								'jumlah_barang' => $this->input->post('jumlah_barang'),
								'nama_pengaju' => $this->input->post('nama_pengaju'),
								'nama_unit' => $this->input->post('nama_unit'),
								'tanggal_pengajuan' => $this->input->post('tanggal_pengajuan'),
								'kode_barang' => $this->input->post('kode_barang'),
								'id_perangkat' => $this->input->post('id_perangkat'),
								'jenis_kebutuhan' => $this->input->post('jenis_kebutuhan'),
								'uraian_pengajuan' => $this->input->post('uraian_pengajuan'),
								'tanggal_estimasi' => $this->input->post('tanggal_estimasi'),
							);

				$data['tanggal_pengajuan']		= date_format3(post('tanggal_pengajuan'), 'YYYY-MM-DD');
				$data['tanggal_estimasi']		= date_format3(post('tanggal_estimasi'), 'YYYY-MM-DD');

				if ($this->input->post('update_stock') != null){
					$DataStatus = array('jumlah' => $this->input->post('update_stock'),
										'stock_persediaan' => $this->input->post('update_stock')
										);
					$this->INVENTORY_PERANGKAT->UpdateStatus($DataStatus, array('id_perangkat' => $this->input->post('id_perangkat'),));
				} else {
					$notif="- No Record -";
					$DataStatus = array(
										'stock_persediaan' => $notif
										);
					$this->INVENTORY_PERANGKAT->UpdateStatus($DataStatus, array('id_perangkat' => $this->input->post('id_perangkat'),));
				}

				if (post("mode") == "create") {
					//$data['tgl_rekam']	= NOW;
					//$data['user_rekam']	= USERID;

					$this->INVENTORY_PERANGKAT->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.app_id ?? ""));

				} else if (post("mode") == "edit") {
					//$data['tgl_ubah']	= NOW;
					//$data['user_ubah']	= USERID;
					
					$this->INVENTORY_PERANGKAT->Update($data, 
										array('id_pengadaan'	=> $data['id_pengadaan']));

					$ls_response = "Data Tersimpan[Edited]";
					//App.audit_trail("CS002", "edit/" + (data.app_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete() {
		$ls_result	= "";
		$pesan 			= "Data Terhapus";
		$kodepengadaan	= get('id_pengadaan');
		if ($kodepengadaan != "") {
			// Delete Single
			$arr_rule	= array('id_pengadaan' => $kodepengadaan);
			$ls_result = $this->INVENTORY_PERANGKAT->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_pengadaan' => $row['id_pengadaan']);
				$ls_result .= $this->INVENTORY_PERANGKAT->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}

}
?>